/*
 * Created on 2006-9-28
 *
 */
package edu.pku.sei.modeler.locator;

import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.CTabFolder;
import org.eclipse.swt.custom.CTabItem;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Label;

import edu.pku.sei.modeler.model.structure.PortLocatorAdapter;

/**
 * @author zhangle
 *
 */
public class PortLocatorItem extends CTabItem {
	private PortLocatorAdapter locator;
	private Group directionGroup;
	private Group numPerSideGroup;
	
	public PortLocatorItem(CTabFolder parent, int style, PortLocatorAdapter locator) {
		super(parent, style);
		this.locator = locator;
	}
	
	public void createControl() {
		final Composite container = new Composite(getParent(), SWT.NONE);
		setControl(container);
		final GridLayout gridLayout = new GridLayout();
		gridLayout.verticalSpacing = 5;
		gridLayout.marginHeight = 5;
		gridLayout.numColumns = 3;
		container.setLayout(gridLayout);
		
		Label label = new Label(container, SWT.NONE);
		label.setText("Description: ");
		label.setLayoutData(new GridData());
		
		label = new Label(container, SWT.NONE);
		label.setText("auto-arrange the bounds as ports on class");
		
		createDirectionGroup(container);
		
		//createBoundNumGroup(container);
		
		createNumPerSideGroup(container);		
	}
	
	protected void createDirectionGroup(Composite container) {
		GridData gridData = new GridData(GridData.FILL_HORIZONTAL);
		gridData.horizontalSpan = 3;
		directionGroup = new Group(container, SWT.NONE);
		directionGroup.setText("Direction");
		directionGroup.setLayoutData(gridData);
		GridLayout layout = new GridLayout();
		layout.marginHeight = 10;
		layout.marginWidth = 10;
		layout.numColumns = 4;
		directionGroup.setLayout(layout);
		
		Label label = new Label(directionGroup, SWT.NONE);
		label.setText("Direction of bounds:");
		final Combo dirCombo = new Combo(directionGroup, SWT.READ_ONLY);
		dirCombo.add("WEST");
		dirCombo.add("SOUTH");
		dirCombo.add("EAST");		
		dirCombo.add("NORTH");		
		dirCombo.add("SURROUNDING");
		if(locator != null) {
			dirCombo.select(locator.getDirection());
		}
		else dirCombo.select(0);
		dirCombo.setLayoutData(new GridData(GridData.FILL_HORIZONTAL
				| GridData.GRAB_HORIZONTAL));
		dirCombo.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(SelectionEvent e) {
				if(locator == null)return;
				locator.setDirection(dirCombo.getSelectionIndex());
				if(dirCombo.getSelectionIndex() == 4) {
					Control [] c = numPerSideGroup.getChildren();
					for(int i=0; i<c.length; i++) {
						c[i].setEnabled(true);
					}
				}
				else {
					Control [] c = numPerSideGroup.getChildren();
					for(int i=0; i<c.length; i++) {
						c[i].setEnabled(false);
					}
				}
			}
		});
	}
	 
	protected void createNumPerSideGroup(Composite container) {
		GridData gridData = new GridData(GridData.FILL_HORIZONTAL);
		gridData.horizontalSpan = 3;
		numPerSideGroup = new Group(container, SWT.NONE);
		numPerSideGroup.setText("Number Per Side");
		numPerSideGroup.setLayoutData(gridData);
		
		GridLayout layout = new GridLayout();
		layout.marginHeight = 10;
		layout.marginWidth = 10;
		layout.numColumns = 3;
		numPerSideGroup.setLayout(layout);
		
		Label label = new Label(numPerSideGroup, SWT.NONE);
		label.setText("Num of bounds on each side(for SURROUNDING only):");
		final Combo numCombo = new Combo(numPerSideGroup, SWT.READ_ONLY);
		for(int i=1; i<10; i++) {
			numCombo.add(String.valueOf(i));
		}
		numCombo.setEnabled(false);
		if(locator != null) {
			numCombo.select(locator.getNumPerSide()-1);
			if(locator.getDirection()==PortLocatorAdapter.SURROUNDING) {
				numCombo.setEnabled(true);
			}
		}
		else {
			numCombo.select(0);
		}
		numCombo.setLayoutData(new GridData(GridData.FILL_HORIZONTAL
				| GridData.GRAB_HORIZONTAL));
		numCombo.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(SelectionEvent e) {
				if(locator == null)return;
				locator.setNumPerSide(numCombo.getSelectionIndex()+1);
			}
		});
	}
}
